<?php
/**
 * 重庆柯一网络有限公司
 * 遵循MT协议，开源并可商业使用，没有任何限制
 * @Author:cqkyi
 * @Date: 2020/9/14 14:38
 * 官方惟一地址：www.cqkyi.com
 */

namespace app\common\service;


class TreeService
{


    public static  function MakeUrlTree($list,$pk,$pid,$name,$child='children',$root=0){
        $tree=[];

        foreach ($list as $k=>$v){
            if($v[$pid]==$root){
                $node=[
                    'id'=>$v[$pk],
                    'parentId'=>$v[$pid],
                    'url'=>$v['permission'],
                    'name'=>$v[$name],
                    'icon'=>$v['node_icon']
                ];
                unset($list[$k]);
                if(!empty($list)){
                    $child=self::MakeUrlTree($list,$pk,$pid,$name,$child,$v[$pk]);
                    if(!empty($child)){
                        $node['children']=$child;
                }

                }
                $tree[]=$node;
            }
        }
        return $tree;
    }



    public static function MakeMenuTree($list,$pk,$pid,$child='children',$root=0){
        $tree=[];

        foreach ($list as $k=>$v){

            if($v[$pid]==$root){


                $node=[
                    'id'=>$v[$pk],
                    'parentId'=>$v[$pid],
                    'component'=>$v['component'],
                    'path'=>$v['path'],
                    'name'=>$v['menu_name'],
                    'perms'=>$v['perms'],
                    'icon'=>$v['icon'],
                    'status'=>$v['status'],
                    'visible'=>$v['visible'],
                    'order_num'=>$v['order_num'],
                    'menu_type'=>$v['menu_type'],

                ];
                unset($list[$k]);
                if(!empty($list)){
                    $child=self::MakeMenuTree($list,$pk,$pid,$child,$v[$pk]);
                    if(!empty($child)){
                        $node['children']=$child;

                    }

                }
                $tree[]=$node;
            }
        }
        return $tree;
    }



    public static function MakeTreePid($list,$pk,$pid,$child='children',$root=0){
        $tree=[];

        foreach ($list as $k=>$v){

            if($v[$pid]==$root){


                $node=[
                    'id'=>$v[$pk],
                    'name'=>$v['menu_name'],


                ];
                unset($list[$k]);
                if(!empty($list)){
                    $child=self::MakeTreePid($list,$pk,$pid,$child,$v[$pk]);
                    if(!empty($child)){
                        $node['children']=$child;

                    }

                }
                $tree[]=$node;
            }
        }
        return $tree;
    }



}